package com.ht.api.call.func.impl.transfer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbConfig;
import com.ht.api.db.Entity;
import com.ht.api.db.task.HtTaskExecutor;
import com.ht.api.db.util.HtUtil;
import com.ht.api.task.TagInitTask;
import com.ht.api.util.UserUtil;

import javax.sql.DataSource;
import java.util.Arrays;

/**
 * 获取接替人员可继承的最大数量
 */
public class GetTakeoverUserInheritNumFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "获取接替人员可继承的最大数量";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		String takeoverUserid = param.getStr("takeover_userid");
		String appid = param.getStr("appid");

		long followNum = Db.createSqlParser("wx_workuser_customer").where("userid", takeoverUserid).where("tenant_appid", appid)
				.where("status", "in", Arrays.asList(0,2)).count(me.getDataSource());

		long waitTransferNum = Db.createSqlParser("wx_transfer_user_item").where("appid", appid)
				.where("takeover_userid", takeoverUserid).where("status", "in", Arrays.asList(0,1,6)).count(me.getDataSource());

		long inheritNum = 20000 - followNum - waitTransferNum;

		return JSONUtil.createObj().set("inheritNum", inheritNum);
	}

}
